package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.domain.StrategyCatalog;
import cn.wolfcode.wolf2w.domain.StrategyTheme;
import cn.wolfcode.wolf2w.query.StrategyQuery;
import cn.wolfcode.wolf2w.service.IStrategyCatalogService;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.service.IStrategyThemeService;
import cn.wolfcode.wolf2w.util.JsonResult;
import cn.wolfcode.wolf2w.vo.CatalogVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.ParseException;
import java.util.List;

@Controller
@RequestMapping("strategy")
public class StrategyController {

    @Autowired
    private IStrategyService strategyService;

    @Autowired
    private IStrategyCatalogService strategyCatalogService;

    @Autowired
    private IStrategyThemeService strategyThemeService;

    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") StrategyQuery qo){
        Page<Strategy> page = strategyService.query(qo);
        model.addAttribute("page", page);
        return "strategy/list";
    }

    @RequestMapping("/input")
    public String input(String id, Model model){
        model.addAttribute("catalogs", strategyCatalogService.groupList());
        List<StrategyTheme> themes = strategyThemeService.selectAll();
        model.addAttribute("themes", themes);
        if (StringUtils.hasLength(id)) {
            model.addAttribute("strategy", strategyService.selectById(id));
        }

        return "strategy/input";
    }

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(Strategy strategy) throws ParseException {
        strategyService.saveOrUpdate(strategy);
        return JsonResult.success();
    }

    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(String id){

        if (StringUtils.hasLength(id)) {
            strategyService.deleteById(id);
        }

        return JsonResult.success();
    }

    /*
        @RequestMapping("/changeState")
        @ResponseBody
        public Object changeState(int state, String id){
            Strategy strategy = strategyService.setChangeState(state, id);
            return JsonResult.success(strategy);
        }
    */
    @RequestMapping("/changeState")
    @ResponseBody
    public Object changeState(int state, String id){
        strategyService.setChangeState(state, id);
        return JsonResult.success();
    }

}
